/***************************************************************************************************
 *Copyright(C),2010-2016,Sumscope
 *FileName	:  pinyin_search_buffer.cpp
 *Author	:  scofined.qi
 *Version	:  1.0
 *Date		:  2017/06/05
 *Desc		:  //用于主要说明此程序文件完成的主要功能
 *Relation :
 *Others	:  //其他内容说明
 *Function :  //主要函数列表，每条记录应包含函数名及功能简要说明
 *History	:
 * //修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简介
 ***************************************************************************************************/
#include "pinyin_search_buffer.h"

#include "../string_page.h"

#ifndef BASSERTS
#define BASSERTS  //
#endif
#ifndef BASSERT
#define BASSERT  //
#endif

typedef qb::base::StringPage StringPage;
namespace qb {
namespace base {
namespace internal {

BondChangeBuffer::BondChangeBuffer() { m_page = new StringPage(); }

BondChangeBuffer::~BondChangeBuffer() {
  m_items.clear();
  if (m_page) delete m_page;
}

void BondChangeBuffer::Reset() {
  m_items.clear();
  m_page->Reset();
}
void BondChangeBuffer::Reserve(int ncount) { m_items.reserve(ncount); }
void BondChangeBuffer::AddChange(int id, kChangeType type, const char* oldName,
                                 int oldLen, const char* name, int len) {
  BASSERTS(type > kChangeTypeNone && type < kChangeTypeCount, "");
  ChangeItem ci;
  ci.id = id;
  ci.type = type;
  ci.old = m_page->AddString(oldName, oldLen);
  ci.now = m_page->AddString(name, len);
  ci.oldlen = oldLen;
  ci.nowlen = len;
  m_items.emplace_back(ci);
}
void BondChangeBuffer::AddNameChange(int id, const char* oldName, int oldLen,
                                     const char* name, int len) {
  AddChange(id, kChangeTypeName, oldName, oldLen, name, len);
}

void BondChangeBuffer::AddNameEnChange(int id, const char* oldName, int oldLen,
                                       const char* name, int len) {
  AddChange(id, kChangeTypeNameEn, oldName, oldLen, name, len);
}

void BondChangeBuffer::AddCodeChange(int id, const char* oldCode, int oldLen,
                                     const char* code, int len) {
  AddChange(id, kChangeTypeCode, oldCode, oldLen, code, len);
}

int BondChangeBuffer::GetChange(int index, kChangeType& type, const char*& old,
                                const char*& now) const {
  BASSERT(index >= 0 && index < (int)m_items.size());
  const ChangeItem& ci = m_items[index];
  type = ci.type;
  old = ci.old;
  now = ci.now;
  return ci.id;
}
const BondChangeBuffer::ChangeItem& BondChangeBuffer::GetChangeItem(
    int index) const {
  BASSERT(index >= 0 && index < (int)m_items.size());
  return m_items[index];
}
}  // namespace internal
}  // namespace base
}  // namespace qb
